﻿using System;
using System.IO;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_NodeDiaDiem_AddEditDiaDiem : System.Web.UI.Page
{
    public static int _id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // litResult.Text = "<p class=\"alert alert-success\">Hi</p>";
            var id = Request.QueryString["Id"];
            BindDatatoDDL.BindCountry(ddlCountry);
            if (id != null && id != "")
            {
                id = id.Replace("'", "");
                _id = Convert.ToInt32(id);
                BindData(Convert.ToInt32(id));
            }
        }
    }

    public void BindData(int id)
    {
        try
        {
            var controller = new VYC24Controller();
            var list = controller.GetById(id);
            if (list.Count > 0)
            {
                tbTen.Text = list[0].Ten;
                ddlCountry.SelectedValue = Convert.ToString(list[0].IdCountry);
                tbTomtat.Text = Server.HtmlDecode(list[0].TomTat);
                tbNoidung.Text = Server.HtmlDecode(list[0].NoiDung);
                tbDiachi.Text = list[0].Address;
                tbTungdo.Text = list[0].TungDo;
                tbVido.Text = list[0].ViDo;
                imgPopup.ImageUrl = Constants.UPLOAD_DIADIEM + list[0].AnhDaiDien;
                imgPopup.AlternateText = list[0].AnhDaiDien;
                cbTrangThai.Checked = Convert.ToBoolean(list[0].TrangThai);
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Không tồn tại dữ liệu tương ứng.</p>";
            }
        }
        catch (Exception)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Không tồn tại dữ liệu tương ứng.</p>";            
        }
    }

    protected void btnThemvao_OnClickbtnThemvao_OnClickbtnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var controller = new VYC24Controller();
            var model = new ModelVYC24();

            model.Ten = tbTen.Text.Trim().Replace("'","''");
            model.IdCountry = Convert.ToInt32(ddlCountry.SelectedValue);
            model.UniqueId = Common.ConvertToUnSign(tbTen.Text.Trim());
            model.TomTat = Server.HtmlEncode(tbTomtat.Text.Replace("'", "''"));
            if (fuAnhdaidien.HasFile)
            {
                var pathAnhbia = Server.MapPath(Constants.UPLOAD_DIADIEM);
                if (!Directory.Exists(pathAnhbia))
                {
                    Directory.CreateDirectory(pathAnhbia);
                }
                // validate file image
                string[] validFileTypes = { "gif", "png", "jpg", "jpeg" };
                var extFile = Path.GetExtension(fuAnhdaidien.PostedFile.FileName);
                var isValidate = false;

                if (extFile != null)
                {
                    foreach (var type in validFileTypes)
                    {
                        if (extFile.Equals("." + type))
                        {
                            isValidate = true;
                            break;
                        }
                    }
                }

                if (!isValidate)
                {
                    litResult.Text = "Invalid file with extension!";
                    model.AnhDaiDien = Constants.NO_IMAGE;
                }
                else
                {

                    litResult.Text = "Ok!";
                    // Luu file 
                    fuAnhdaidien.SaveAs(pathAnhbia + Common.ConvertToUnSign(tbTen.Text) + "_diadiem_" + DateTime.Now.ToString("ddMMyyhhmmss") +
                                    extFile);
                    model.AnhDaiDien = Common.ConvertToUnSign(tbTen.Text) + "_diadiem_" + DateTime.Now.ToString("ddMMyyhhmmss") + extFile;
                }
            }
            else
            {
                model.AnhDaiDien = imgPopup.AlternateText;
                // model.AnhDaiDien = Constants.NO_IMAGE;
            }
            model.NoiDung = Server.HtmlEncode(tbNoidung.Text);
            model.Address = tbDiachi.Text;
            model.TungDo = tbTungdo.Text;
            model.ViDo = tbVido.Text;
            model.TrangThai = Convert.ToInt32(cbTrangThai.Checked);
            var isSuccess = 0;
            if (_id != null && _id != 0)
            {
                // cap nhat
                model.Id = _id;
                isSuccess = controller.EditData(model);
            }
            else
            {
                isSuccess = controller.AddData(model);
            }
            if (isSuccess == 0)
            {
                // Chua thanh cong                
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-success\">Cập nhật dữ liệu thành công.</p>";
                ResetInput();
            }

        }
        catch (Exception)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Cập nhật dữ liệu thất bại.</p>";
            throw;
        }
    }
    
    public void ResetInput()
    {
        tbTen.Text = "";
        tbNoidung.Text = "";
        tbTomtat.Text = "";
        tbDiachi.Text = "";
        ddlCountry.SelectedValue = "0";
        tbTungdo.Text = "";
        tbVido.Text = "";
        cbTrangThai.Checked = false;
        _id = 0;
    }

}